window.onload=function () {
    var judge = true;
    if (localStorage.getItem('userHao') == null) {
        window.location = 'http://localhost:8080/future/admin/querySinger';
    }
    var music = document.getElementsByClassName('music')[0];
    var musicForm = document.getElementsByClassName('musicForm')[0];
    music.onclick = function () {
        if (judge == true) {
            musicForm.style.display = 'block';
            judge = false;
            console.log();
        } else {
            musicForm.style.display = 'none';
            judge = true;
        }
    }

    //头像处的列表的出现
    var headPhoto = document.getElementsByClassName('headPhoto')[0];
    var personage = document.getElementsByClassName('personage')[0];
    var xia2 = document.getElementsByClassName('xia2')[0];
    headPhoto.innerHTML = localStorage.getItem('userHao');
    headPhoto.onclick = function () {
        if (judge == true) {
            console.log("aaaaa");
            personage.style.height = '8%';
            judge = false;
        } else {
            personage.style.height = '0%';
            judge = true;
        }

    }


    //退出登录
    var back = document.getElementsByClassName('back')[0];
    back.onclick = function () {
        var flag = confirm("是否确定要退出登录", "确定");
        if (flag) {
            window.location.href = 'http://localhost:8080/future/loginpage';
            localStorage.removeItem('userHao');//清除保留的用户名
        }

    }
    //查询歌曲
    var input = document.getElementsByClassName('input')[0];//用户名的输入框
    var ssBtn = document.getElementsByClassName('ssBtn')[0];//搜索的按钮
    var Apage=document.getElementsByClassName('pageAll')[0];
    ssBtn.onclick = function () {
        $.ajax({
            url: 'http://localhost:8080/future/admin/querySingerByusername',
            method: "GET",
            data: {//参数
                userName: input.value,
            },
            success: function (data) {
                console.log(data.length);
                document.getElementsByClassName('content')[0].innerHTML = '';
                var a=data.length;
                Math.ceil(a / 10); //向上整除 4/3=2;
                console.log(  Math.ceil(a / 10));
                for (var index = 0; index < data.length; index++) {
                    var element=data[index]
                    var i = index + 1;
                    document.getElementsByClassName("content")[0].innerHTML += "<tr class='found'>" + "<td class='find'>" + "<input class='choice' type='checkbox' name='ids'>" + "</td>" + "<td class='find'>" + i + "</td>" + "<td class='find'>" + element.userName + "</td>" + "<td class='find'>" + element.userHao + "</td>" + "<td class='find'>" + element.userGen + "</td>" + "<td class='find'>" + element.userEmail + "</td>" +
                        "<td class='find'>" + element.userHot + "</td>" + "<td class='find'>" + element.userVipBal + "</td>" +
                        "<td class='find'>" + "<button class='del'>删除</button>" + "<button class='amend'>更改</button>" + "</td>" + "</tr>"
                    console.log(data[0].userName);

                }
                console.log(Apage.innerHTML)
                Apage.innerHTML=  Math.ceil(a / 10);
            },
            error: function (err) {//请求失败，执行的回调函数
                console.log('错误');
            }
        })
    }


    // 新增的弹窗
    var addUser = document.getElementsByClassName('addUser')[0];
    var add = document.getElementsByClassName('add')[0];
    var affirm = document.getElementsByClassName('affirm')[0];
    var cancel = document.getElementsByClassName('cancel')[0];
    var reset = document.getElementsByClassName('reset')[0];
    add.onclick = function () {
        addUser.style.display = 'block';
    }
    affirm.onclick = function () {
        addUser.style.display = 'none';
    }
    cancel.onclick = function () {
        addUser.style.display = 'none';
    }
    reset.onclick = function () {
        window.location.href = "http://localhost:8080/future/admin/querySinger";//页面刷新
    }

    //新增一名歌手
  var email=document.getElementsByClassName('email');
    var selectGen=document.getElementsByClassName('selectGen')[0];

    affirm.onclick = function () {
        // console.log(selectGen.value);
        // console.log(email[7].value);
        $.ajax({
            url: 'http://localhost:8080/future/admin/addSinger',
            method: 'GET',
            data: {
                userBirth:email[0].value,
                userEmail:email[1].value,
                userGen:selectGen.value,
                userHao:email[3].value,
                userHot:email[4].value,
                userImgload:email[5].value,
                userName:email[6].value,
                userPwd:email[7].value,
                userThing:email[2].value,
            },
            success: function (data) {

                console.log(data);//控制台显示返回的数据
                location.reload();//添加成功则进行页面刷新

            },
            error: function (err) {//请求失败，执行的回调函数

                console.log('错误');
            }

        })
    }
    //删除一名歌手

    var del = document.getElementsByClassName('del');
    for (var i = 0; i < del.length; i++) {
        del[i].onclick = function () {
            // $('.content').on('click', '.del', function () {//查询到要获取到的元素
            var flag = confirm("是否要删除该名歌手", "确定");
            // console.log(this.parentNode.parentNode.getAttribute("everid"));//获取当前行的id
            // var xuHao = this.parentNode.parentNode.children[1].innerHTML;
            if (flag) {

                $.ajax({
                    url: "http://localhost:8080/future/admin/deleteUser",
                    type: "GET",
                    data: {
                        userId: this.parentNode.parentNode.getAttribute("everid"),
                    },
                    success: function (data) {
                        if (data.statusCode === 1) {
                            alert("删除成功")
                            window.location.reload();
                        } else {
                            alert("删除失败");
                            // console.log(data.info);
                        }


                    },
                    error: function (err) {
                        console.log("错误");
                    },
                })
            }
        }
    }
//修改歌手信息
    var change=document.getElementsByClassName('amendUser   ')[0];
    var affirm_2 = document.getElementsByClassName('affirm_2')[0];
    var cancel_2 = document.getElementsByClassName('cancel_2')[0];
    var information_2=document.getElementsByClassName('information_2');
    var amend=document.getElementsByClassName('amend');
    var userBirth=document.getElementsByClassName('userBirth')[0];
    var userEmail=document.getElementsByClassName('userEmail')[0];
    var userHao=document.getElementsByClassName('userHao')[0];
    var userHot=document.getElementsByClassName('userHot')[0];
    var userImgload=document.getElementsByClassName('userImgload')[0];
    var userPwd=document.getElementsByClassName('userPwd')[0];
    var userName=document.getElementsByClassName('userName')[0];
    var userThing=document.getElementsByClassName('userThing')[0];
    var selectLv=document.getElementsByClassName('selectLv')[0];
    var VipDay=document.getElementsByClassName('VipDay')[0];
    for(var i=0;i < amend.length;i++){
        amend[i].onclick=function (){

            userBirth.value=this.parentNode.parentNode.getAttribute("userbirth");
            userEmail.value=this.parentNode.parentNode.children[5].innerHTML;
            userHao.value=this.parentNode.parentNode.children[3].innerHTML;
            userHot.value=this.parentNode.parentNode.children[6].innerHTML;
            userImgload.value=this.parentNode.parentNode.getAttribute("userImgload");
            userPwd.value=this.parentNode.parentNode.getAttribute("userPwd");
            userName.value=this.parentNode.parentNode.children[2].innerHTML;
            userThing.value=this.parentNode.parentNode.getAttribute("userThing");
            selectGen.value=this.parentNode.parentNode.children[4].innerHTML;
            selectLv.value=this.parentNode.parentNode.getAttribute("userLv");
            VipDay.value=this.parentNode.parentNode.getAttribute("VipDay");

            var id=this.parentNode.parentNode.getAttribute("everid");
            change.style.display = 'block';
            affirm_2.onclick = function () {
                var flag = confirm("是否确认要修改当前音乐的信息", "确认");
                if (flag) {
                    alter();
                    change.style.display = 'none';
                } else {
                    change.style.display = 'none';
                }

            }
            cancel_2.onclick = function () {
                change.style.display = 'none';
            }

            function alter() {
                $.ajax({
                    url: 'http://localhost:8080/future/admin/updateUser',
                    method: 'GET',
                    data: {
                        userId:id,
                        userBirth:userBirth.value,
                        userEmail: userEmail.value,
                        userGen: selectGen.value,
                        userName:userName.value,
                        userThing:userThing.value,
                        userImgload:userImgload.value,
                        userLv:selectLv.value,
                        userVipBal:  VipDay.value,
                    },
                    dataType: "json",
                    success: function (data) {

                        // location.reload();
                        console.log(data);
                        // if(data.statusCode==0){
                        //     alter(data.info);
                        // }
                        location.reload();
                    },
                    error: function (err) {//请求失败，执行的回调函数
                        console.log('错误');
                    }
                })
            }

        }
    }
    //全选
    //点击获取下面全部的信息，再点取消
    var all=document.getElementById('all');
    function DoCheck()
    {
        var ch=document.getElementsByName("ids");
        if(document.getElementsByName("allChecked")[0].checked==true) {
            for(var i=0;i<ch.length;i++) {
                ch[i].checked=true;
            }
        }else{
            for(var i=0;i<ch.length;i++)
            {
                ch[i].checked=false;
            }
        }
    }
    all.onclick=DoCheck;

    //分页查询
    var previous=document.getElementsByClassName('previous')[0];
    var next=document.getElementsByClassName('next')[0];
    var page=1;
    var toPage=document.getElementsByClassName('toPage')[0];
    var pageAll=document.getElementsByClassName('pageAll')[0].innerHTML
    var first=document.getElementsByClassName('first')[0];
    var end=document.getElementsByClassName('end')[0];
    first.onclick=function () {
        location.href = 'http://localhost:8080/future/admin/querySinger?page=1';
    }
    end.onclick=function (){
        location.href = 'http://localhost:8080/future/admin/querySinger?page='+pageAll;
    }
    previous.onclick=function(){
        if(toPage.value<=1){
            alert("这已经是最前面一页！");
            return;
        }else{
            page=toPage.value-1;
            location.href = 'http://localhost:8080/future/admin/querySinger?page='+page;
        }

    }
    next.onclick=function(){
        console.log(pageAll);
        var pagenum = pageAll;
        if(Number(toPage.value)>=pagenum){
            alert("没有下一页了！");
            return;
        }else{
            page=Number(toPage.value)+1;

            location.href = 'http://localhost:8080/future/admin/querySinger?page='+page;
            console.log(page);
        }
    }

    //更改页码框中的值
    toPage.onkeydown=function(event){
        var event=event;
        // console.log(event.keyCode)
        if(event.keyCode==13){
            location.href = 'http://localhost:8080/future/admin/querySinger?page='+Number(toPage.value);
        }
    }

//批量删除

    $(function(){
        $(".batch").click(function(){
            //判断是否至少选择一项
            var checkedNum = $("input[name='numbers']:checked").length;
            console.log(checkedNum);
            if(checkedNum==0){
                alert("请至少选择一项进行删除");
                return;
            }
            if(confirm("确定要删除所选项目？")){
                //这个数组存储要删除的数据的id
                var checkedList = [];
                //每一个被选中的name为ids的input标签
                $("input[name='numbers']:checked").each(function(){
                    checkedList.push(Number($(this).val()));//将id存入数组
                    console.log(checkedList);

                });
                $.ajax({
                    type:"GET",
                    dataType:"json",
                    // contentType:"application/json",
                    url:"http://localhost:8080/future/admin/BatchdeleteUsers",//不同页面只用更改action即可
                    async:true,
                    //数组的toString里的id是用","分隔开的，例如数组{"1","2","3"},同String后为"1,2,3"
                    data:{
                        numbers:JSON.stringify(checkedList),
                    },
                    success:function(data){
                        console.log(data)
                        alert(data.info);
                        window.location.reload();
                    },
                    error:function(){
                        alert("错误");
                    }
                })
            }
        })

    })























}